package de.tum.in.eist.poll.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;

import de.tum.in.eist.poll.shared.Student;
import de.tum.in.eist.poll.shared.exceptions.*;
import de.tum.in.eist.poll.shared.Validation;

/***
 * view for student for logging in
 */
public class StudentLoginView {
	static void paint(){
		if(Poll.student != null){
			History.newItem("studentSurvey");
			return;
		}
		RootPanel.get("logo").removeStyleName("logo-small");
		final TextBox idField = new TextBox(); 	// TextBox for entering 'Matrikelnummern' and displaying errors
		idField.getElement().setAttribute("type", "tel");
		idField.addStyleName("idField");
		idField.getElement().setAttribute("placeholder", "Student ID");
		final Button sendButton = new Button("Send"); 	// Button for submiting input
		sendButton.addStyleName("sendButton");
		// headline of the login-page
		final HTML loginHint = new HTML("Your feedack is very important for us!<br>"+
				"Authenticate with your studend id and fill the survey.<br>(e.g.: Student-ID: 01234567)");
		loginHint.setStyleName("loginHint");
		class LoginHandler implements ClickHandler, KeyUpHandler {
			public void onClick(ClickEvent event) {
				authToServer();
			}
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					authToServer();
				}
			}
			/**
			 * Validates the matrnr provided by the login
			 * @param input
			 * @return
			 */
			public boolean isValidLoginInput(String input) {
				
				switch(Validation.validateStudentLogin(input))
				{
					case 1:
						// set errorLabel and add it to the RootPanel
						idField.setText("");
						idField.setFocus(false);
						idField.getElement().setAttribute("placeholder", "Only digits allowed!");
						return false;
					case 2:
						idField.setText("");
						idField.setFocus(false);
						idField.getElement().setAttribute("placeholder", "Your input is too short!");
						return false;
					case 3:
						idField.setText("");
						idField.setFocus(false);
						idField.getElement().setAttribute("placeholder", "Your input is too long!");
						return false;
					default:
						return true;
				}
			}
			public void authToServer() {
				// check if the idField is a valid input
				// if not, set error text and try again
				if (!isValidLoginInput(idField.getText())) {
					return;
				}
				try{
					Poll.authService.authenticateStudent(idField.getText(), "", new AsyncCallback<Student>() {
						public void onFailure(Throwable caught) {
							idField.setText("");
							idField.setFocus(false);
							idField.getElement().setAttribute("placeholder", "Network-Error");
						}
						public void onSuccess(Student result) {
							Poll.student = result;
							History.newItem("studentSurvey");
						}
					});
				}catch(UserNotFoundException e){
					idField.setText("");
					idField.setFocus(false);
					idField.getElement().setAttribute("placeholder", "Login-Error");
				}
			}
		}

		// Add handler for sending student id to the server
		LoginHandler handler = new LoginHandler();
		sendButton.addClickHandler(handler);
		idField.addKeyUpHandler(handler);

		// Add the headline, nameField and sendButton to the RootPanel
		// Use RootPanel.get() to get the entire body element
		RootPanel.get("container").add(loginHint);
		RootPanel.get("container").add(idField);
		RootPanel.get("container").add(sendButton);
		idField.setText("01234567");
	}
}
